//
//  Find_ContentCreateTopicView.m
//  SoundPill
//
//  Created by 宋旭东 on 16/5/17.
//  Copyright © 2016年 宋旭东. All rights reserved.
//

#import "Find_ContentCreateTopicView.h"
#import "AFNetworking.h"
#import "UIViewExt.h"
#import "JCAlertView.h"
#import "ZJSwitch.h"
#import "AppDelegate.h"

@interface Find_ContentCreateTopicView ()
{
    BOOL privateCheckON;
    BOOL unPrivateCheckON;
}
@property (strong, nonatomic) UIButton *unPrivate;
@property (strong, nonatomic) UIButton *private;
@end

@implementation Find_ContentCreateTopicView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}
- (void)createSubviews
{
    self.topicNameTextview = [[UITextView alloc] initWithFrame:CGRectMake(20, 64, self.frame.size.width - 40, 45)];
    _topicNameTextview.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.300];
    _topicNameTextview.textColor = [UIColor grayColor];
    _topicNameTextview.returnKeyType = UIReturnKeyNext;
    _topicNameTextview.layer.borderWidth = 2;
    _topicNameTextview.layer.borderColor = [[UIColor whiteColor] CGColor];
    _topicNameTextview.font = [UIFont systemFontOfSize:18];
    _topicNameTextview.delegate = self;
    _topicNameTextview.text = @"#话题名称";
    [self addSubview:_topicNameTextview];
    
    self.topicDetailTextview = [[UITextView alloc] initWithFrame:CGRectMake(_topicNameTextview.left, _topicNameTextview.bottom + 20, _topicNameTextview.width, _topicNameTextview.height * 5)];
    _topicDetailTextview.backgroundColor = _topicNameTextview.backgroundColor;
    _topicDetailTextview.text = @"#话题描述";
    _topicDetailTextview.textColor = [UIColor grayColor];
    _topicDetailTextview.font = [UIFont systemFontOfSize:15];
    _topicDetailTextview.layer.borderWidth = 2;
    _topicDetailTextview.layer.borderColor = [[UIColor whiteColor] CGColor];
    _topicDetailTextview.returnKeyType = UIReturnKeyDone;
    _topicDetailTextview.delegate = self;
    [self addSubview:_topicDetailTextview];
    
    self.topicDetailLengthLable = [[UILabel alloc] initWithFrame:CGRectMake(_topicDetailTextview.width - 40, _topicDetailTextview.height - 40, 40, 40)];
    _topicDetailLengthLable.textAlignment = NSTextAlignmentCenter;
    _topicDetailLengthLable.textColor = [UIColor grayColor];
    _topicDetailLengthLable.text = @"70";
    [_topicDetailTextview addSubview:_topicDetailLengthLable];
    
    
//    UIColor *onTintColor = [UIColor blackColor];
//    UIColor *tintColor = [UIColor blackColor];
//    UIColor *thumbTintColor = [UIColor whiteColor];
//    ZJSwitchStyle style = ZJSwitchStyleNoBorder;
//    ZJSwitch *switch0 = [[ZJSwitch alloc] initWithFrame:CGRectMake(_topicDetailTextview.left, _topicDetailTextview.bottom + 20, 140, 60)];
//    switch0.style = style;
//    switch0.onTintColor = onTintColor;
//    switch0.tintColor = tintColor;
//    switch0.thumbTintColor = thumbTintColor;
//    switch0.onText = @"私有";
//    switch0.offText = @"公开";
//    [switch0 addTarget:self action:@selector(handleSwitchEvent:) forControlEvents:UIControlEventValueChanged];
//    [self addSubview:switch0];
    
    self.private = [UIButton buttonWithType:UIButtonTypeCustom];
    _private.frame = CGRectMake(_topicDetailTextview.left, _topicDetailTextview.bottom + 20, 40, 40);
    [_private addTarget:self action:@selector(handleSwitchEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_private setImage:[UIImage imageNamed:@"checkOFF"] forState:UIControlStateNormal];
    privateCheckON = NO;
    [self addSubview:_private];
    if ([UIScreen mainScreen].bounds.size.width == 320) {
        _private.frame = CGRectMake(_topicDetailTextview.left, _topicDetailTextview.bottom + 20, 30, 30);
    }
    UILabel *privateLable = [[UILabel alloc] initWithFrame:CGRectMake(_private.right + 10, _private.top, _topicDetailTextview.width - _private.width - 10, _private.height)];
    privateLable.text = @"创建新话题，邀请好友一起参加";
    privateLable.textColor = [UIColor whiteColor];
    [self addSubview:privateLable];
    
    self.unPrivate = [UIButton buttonWithType:UIButtonTypeCustom];
    _unPrivate.frame = CGRectMake(_private.left, _private.bottom, _private.width, _private.height);
    [_unPrivate setImage:[UIImage imageNamed:@"checkOFF"] forState:UIControlStateNormal];
    [_unPrivate addTarget:self action:@selector(unPrivateCheckClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_unPrivate];
    UILabel *unPrivateLable = [[UILabel alloc] initWithFrame:CGRectMake(_unPrivate.right + 10, _unPrivate.top, _topicDetailTextview.width - _unPrivate.width - 10, _unPrivate.height)];
    unPrivateLable.text = @"创建个人作品集，仅自己可添加作品";
    unPrivateLable.textColor = [UIColor whiteColor];
    [self addSubview:unPrivateLable];
    if ([UIScreen mainScreen].bounds.size.width == 320) {
        privateLable.font = [UIFont systemFontOfSize:14];
        unPrivateLable.font = privateLable.font;
    }
    self.createButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _createButton.frame = CGRectMake(_private.left, _unPrivate.bottom + 10, _topicDetailTextview.width, _private.height);
    _createButton.backgroundColor = [UIColor blackColor];
    [self addSubview:_createButton];
    [_createButton addTarget:self action:@selector(createTopicBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_createButton setTitle:@"创建并录制第一个作品" forState:UIControlStateNormal ];
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelButton setImage:[UIImage imageNamed:@"menuWhiteClose"] forState:UIControlStateNormal];
    _cancelButton.frame = CGRectMake(self.center.x - 27, self.height - 49, 55, 50);
    [_cancelButton addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancelButton];
}
- (void)handleSwitchEvent:(UIButton *)sender
{
    if (privateCheckON == NO) {
        privateCheckON = YES;
        [_private setImage:[UIImage imageNamed:@"checkON"] forState:UIControlStateNormal];
        unPrivateCheckON = NO;
        [_unPrivate setImage:[UIImage imageNamed:@"checkOFF"] forState:UIControlStateNormal];
    } else {
        privateCheckON = NO;
        [_private setImage:[UIImage imageNamed:@"checkOFF"] forState:UIControlStateNormal];
    }
}
- (void)unPrivateCheckClick:(UIButton *)sender
{
    if (unPrivateCheckON == NO) {
        unPrivateCheckON = YES;
        [_unPrivate setImage:[UIImage imageNamed:@"checkON"] forState:UIControlStateNormal];
        privateCheckON = NO;
        [_private setImage:[UIImage imageNamed:@"checkOFF"] forState:UIControlStateNormal];
    } else {
        unPrivateCheckON = NO;
        
        [_unPrivate setImage:[UIImage imageNamed:@"checkOFF"] forState:UIControlStateNormal];
    }
}
- (void)cancelBtnClick:(UIButton *)sender
{
    _cancel();
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"#话题名称"] || [textView.text isEqualToString:@"#话题描述"]) {
        textView.text = nil;
    }
    textView.textColor = [UIColor whiteColor];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView == _topicNameTextview) {
        if (textView.text.length == 0) {
            textView.textColor = [UIColor grayColor];
            textView.text = @"#话题名称";
        }
    } else {
        if (textView.text.length == 0) {
            textView.textColor = [UIColor grayColor];
            textView.text = @"#话题描述";
        }
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (textView == _topicNameTextview) {
        if ([text isEqualToString:@"\n"]) {
            [_topicDetailTextview becomeFirstResponder];
            return NO;
        } else {
            return YES;
        }
    } else {
            if ([text isEqualToString:@"\n"]) {
                [_topicDetailTextview resignFirstResponder];
                return NO;
            } else {
                return YES;
            }
    }
}
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView == _topicDetailTextview) {
        NSInteger number = [textView.text length];
        if (number >= 70) {
            textView.text = [textView.text substringWithRange:NSMakeRange(0, 70)];
            number = [textView.text length];
        }
        number = 70 - number;
        self.topicDetailLengthLable.text = [NSString stringWithFormat:@"%d",number];
    }
}
- (void)createTopicBtnClick:(UIButton *)sender
{
    if (_topicNameTextview.text.length > 0 && ![_topicNameTextview.text isEqualToString:@"#话题名称"] && _topicDetailTextview.text.length > 0 && ![_topicDetailTextview.text isEqualToString:@"#话题描述"]) {
        if (unPrivateCheckON == YES || privateCheckON == YES) {
            [AppDelegate shareDelegate].topicString = _topicNameTextview.text;
            if (![_topicDetailTextview.text isEqualToString:@"#话题描述"]) {
                [AppDelegate shareDelegate].topicDetailString = _topicDetailTextview.text;
            }
            if (unPrivateCheckON == YES) {
//                设为公开
                [AppDelegate shareDelegate].topicIsPrivate = [NSNumber numberWithBool:YES];
            } else if (privateCheckON == YES){
                [AppDelegate shareDelegate].topicIsPrivate = [NSNumber numberWithBool:NO];
            }
            [self isTopicNew];
        } else {
            [JCAlertView showOneButtonWithTitle:@"提示" Message:@"请选择创建新话题 或 个人作品集" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
                
            }];

        }
        
    } else {
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"话题名称 或话题描述 不能为空" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{

        }];
    }
}
- (void)isTopicNew
{
    NSString *url = [NSString stringWithFormat:@"http://123.57.1.143/api/tag/isset?topicName=%@",_topicNameTextview.text];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([[responseObject valueForKey:@"status"] boolValue] == YES) {
            _block();
        } else {
            [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"该话题已存在" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"直接录制" Click:^{
                [AppDelegate shareDelegate].topicIsPrivate = nil;
                [AppDelegate shareDelegate].topicDetailString = nil;
                _block();
            } ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"更换话题" Click:^{
                _topicNameTextview.text = @"#话题名称";
                _topicNameTextview.textColor = [UIColor grayColor];
                _topicDetailTextview.text = @"#话题描述";
                _topicDetailTextview.textColor = [UIColor grayColor];
                _topicDetailLengthLable.text = @"70";
            }];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
   
}
@end
